<?php

namespace App\Admin\Controllers;

use Carbon\Carbon;
use Encore\Admin\Grid;
use \App\Models\GroupCard;
use Encore\Admin\Controllers\AdminController;
use Illuminate\Support\Facades\DB;
use Encore\Admin\Layout\Content;

class GroupCardsController extends AdminController
{
    protected $title = '开团卡记录';

    protected function grid()
    {
        $grid = new Grid(new GroupCard());
	    $grid->model()->orderByDesc('id');
	    
	    if ($userId = request('user_id')) {
		    $grid->model()->where('user_id', $userId)->orderByDesc('id');
	    }

        $grid->column('id', 'ID');
        $grid->column('user_id', '会员')->display(function () {
	        return $this->user ? $this->user->nameText(true) : '';
        });

      	
        $grid->column('reason', '记录来源');
        
        $grid->column('card_expired_at', '过期时间')->display(function () {
	        return (string)$this->card_expired_at;
        });
        $grid->column('created_at', '时间')->display(function () {
	        return (string)$this->created_at;
        });
	
	    $this->gridFilter($grid);
	
	    $grid->disableRowSelector();
	    $grid->disableCreateButton();
	    $grid->disableExport();
	    $grid->disableActions();

	   

        return $grid;
    }
	
	protected function gridFilter(Grid $grid)
	{
		$grid->filter(function (Grid\Filter $filter) {
			$filter->expand();

			

			$filter->column(1/2, function (Grid\Filter $filter) {
				$filter->disableIdFilter();
				$filter->equal('user_id', '会员 ID 查询');
			});
			

		    $filter->column(1/2, function (Grid\Filter $filter) {
				$filter->between('created_at', '日期')->datetime();
			});


		});
	}
}
